package com.github.boluo1230.wechat.dao.impl;

import com.github.boluo1230.wechat.base.config.MongoUtil;
import com.github.boluo1230.wechat.dao.BaseReplyInfoDao;
import com.github.boluo1230.wechat.entity.BaseReplyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

/**
 * @author Code Farmer
 * @date 2019/10/16 20:15
 */
@Repository
public class BaseReplyInfoDaoImpl implements BaseReplyInfoDao {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoUtil mongoUtil;

    public BaseReplyInfo getOneByAppId(String appId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("appId").is(appId));
        return mongoTemplate.findOne(query, BaseReplyInfo.class);
    }

    @Override
    public void insert(BaseReplyInfo baseReplyInfo) {
        mongoTemplate.insert(baseReplyInfo);
    }

    @Override
    public long upsert(BaseReplyInfo baseReplyInfo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("appId").is(baseReplyInfo.getAppId()));
        return mongoUtil.upsertOne(baseReplyInfo, query, BaseReplyInfo.class);
    }


}
